package cn.szzsi.model.mall;

import cn.szzsi.common.util.CommonUtil;
import com.jfinal.kit.JsonKit;

import java.util.ArrayList;
import java.util.List;

/**
 * 实体类 - 商品分类
 * 
 */
public class ProductCategory extends BaseProductCategory<ProductCategory> {

	private static final long serialVersionUID = -9170872348235959429L;
	
	public static final ProductCategory dao = new ProductCategory().use(CommonUtil.ZSHOP_MALL).dao();

	public static final String PATH_SEPARATOR = ",";// 树路径分隔符
	
	public List<ProductCategory> getAll() {
		String sql = "select * from mall_product_category t order by t.orderList asc ";
		List<ProductCategory> allProductCategoryList = dao.find(sql);
		return allProductCategoryList;
		//return recursivProductCategoryTreeList(allProductCategoryList, null, null);
	}
	public List<ProductCategory> getByUid(String uid) {
		String sql = "select * from mall_product_category t where t.uid=?  order by t.orderList asc ";
		List<ProductCategory> allProductCategoryList = dao.find(sql,uid);
		return allProductCategoryList;
		//return recursivProductCategoryTreeList(allProductCategoryList, null, null);
	}

	/**
	 * 查询一级菜单
	 * @param uid
	 * @return
	 */
	public List<ProductCategory> getFirstMenu(String uid) {
		String sql = "select * from mall_product_category t where t.parent_id is null and t.uid=?  order by t.orderList asc ";
		List<ProductCategory> allProductCategoryList = dao.find(sql,uid);
		return allProductCategoryList;
		//return recursivProductCategoryTreeList(allProductCategoryList, null, null);
	}
		
	public List<ProductCategory> getChildren() {
		String sql = "select * from mall_product_category t where t.parent_id = ? order by orderList asc";
		return dao.find(sql,getStr("id"));
	}
	

	public Integer getLevel() {
		return getStr("path").split(PATH_SEPARATOR).length - 1;
	}
	
	public ProductCategory getParent() {
		return dao.findById(getStr("parent_id"));
	}
	
	/**
	 * 获取商品分类树集合;
	 * 
	 * @return 商品分类树集合
	 * 
	 */
	public List<ProductCategory> getProductCategoryTreeList() {
		List<ProductCategory> allProductCategoryList = this.getAll();
		return recursivProductCategoryTreeList(allProductCategoryList, null, null);
	}

	/**
	 *
	 * @return 商户商品分类树集合
     */
	public List<ProductCategory> getBusinessProductCategoryTreeList(String uid) {
		List<ProductCategory> allProductCategoryList = this.getByUid(uid);
		return recursivProductCategoryTreeList(allProductCategoryList, null, null);
	}
	
	// 递归父类排序分类树
	private List<ProductCategory> recursivProductCategoryTreeList(List<ProductCategory> allProductCategoryList, ProductCategory p, List<ProductCategory> temp) {
		if (temp == null) {
			temp = new ArrayList<ProductCategory>();
		}
		for (ProductCategory productCategory : allProductCategoryList) {
			ProductCategory parent = productCategory.getParent();
			
			if ((p == null && parent == null) || (productCategory != null && parent == p || (parent != null && parent.equals(p)))) {
				temp.add(productCategory);
				List<ProductCategory> children = productCategory.getChildren();
				if (children != null && children.size() > 0) {
					recursivProductCategoryTreeList(allProductCategoryList, productCategory, temp);
				}
			}
		}
		return temp;
	}
	
	/**
	 * 获取所有顶级商品分类集合;
	 * 
	 * @return 所有顶级商品分类集合
	 * 
	 */
	public List<ProductCategory> getRootProductCategoryList() {
		String sql = "select * from mall_product_category  where parent_id is null order by orderList asc";
		return dao.find(sql);
	}

	/**
	 * 获取平台所有顶级商品分类集合;
	 *
	 * @return 所有顶级商品分类集合
	 *
	 */
	public List<ProductCategory> getPlatformRootProductCategoryList() {
		String sql = "select * from mall_product_category  where parent_id is null  order by orderList asc";
		return dao.find(sql);
	}

	/**
	 * 获取商户所有顶级商品分类集合;
	 *
	 * @return 所有顶级商品分类集合
	 *
	 */
	public List<ProductCategory> getBusinessRootProductCategoryList(String uid) {
		String sql = "select * from mall_product_category  where parent_id is null and uid = ? order by orderList asc";
		return dao.find(sql,uid);
	}

	
	/**
	 * 根据ProductCategory对象获取所有父类集合，若无父类则返回null;
	 * 
	 * @return 父类集合
	 * 
	 */
	public List<ProductCategory> getParentProductCategoryList(ProductCategory productCategory) {
		String[] ids = productCategory.getStr("path").split(ProductCategory.PATH_SEPARATOR);
		String ids1="";
		for(int i=0;i<ids.length;i++){
			ids1+= JsonKit.toJson(ids[i])+",";

		}
		if(ids1.endsWith(",")){
			ids1= ids1.substring(0,ids1.length()-1);
		}
		String sql = "select * from mall_product_category  where id != ? and id in(?) order by orderList asc";
		return dao.find(sql,productCategory.getStr("id"),ids1);
	}

	/**
	 * 根据ProductCategory对象获取所有父类集合，若无父类则返回null;
	 *
	 * @return 父类集合
	 *
	 */
	public List<ProductCategory> getBusinessParentProductCategoryList(String uid, ProductCategory productCategory) {
		String[] ids = productCategory.getStr("path").split(ProductCategory.PATH_SEPARATOR);
		String ids1="";
		for(int i=0;i<ids.length;i++){
			ids1+= JsonKit.toJson(ids[i])+",";

		}
		if(ids1.endsWith(",")){
			ids1= ids1.substring(0,ids1.length()-1);
		}
		String sql = "select * from mall_product_category  where uid=? and id != ? and id in(?) order by orderList asc";
		return dao.find(sql,uid,productCategory.getStr("id"),ids1);
	}
	
	/**
	 * 根据ProductCategory对象获取路径集合;
	 *
	 * @return ProductCategory集合
	 *
	 */
	public List<ProductCategory> getProductCategoryPathList(ProductCategory productCategory) {
		List<ProductCategory> productCategoryPathList = new ArrayList<ProductCategory>();
		productCategoryPathList.addAll(this.getParentProductCategoryList(productCategory));
		productCategoryPathList.add(productCategory);
		return productCategoryPathList;
	}

	/**
	 * 根据ProductCategory对象获取商户路径集合;
	 *
	 * @return ProductCategory集合
	 *
	 */
	public List<ProductCategory> getBusinessProductCategoryPathList(String uid, ProductCategory productCategory) {
		List<ProductCategory> productCategoryPathList = new ArrayList<ProductCategory>();
		productCategoryPathList.addAll(this.getBusinessParentProductCategoryList(uid,productCategory));
		productCategoryPathList.add(productCategory);
		return productCategoryPathList;
	}


	/**
	 * 根据ProductCategory对象获取所有子类集合，若无子类则返回null;
	 * 
	 * @return 子类集合
	 * 
	 */
	public List<ProductCategory> getChildrenProductCategoryList(ProductCategory productCategory) {
		String sql = "select * from mall_product_category  where id != ? and path like ? order by orderList asc";
		return dao.find(sql,productCategory.getStr("id"),productCategory.getStr("id") + "%");
	}

	/**
	 * 根据ProductCategory对象获取平台所有子类集合，若无子类则返回null;
	 *
	 * @return 子类集合
	 *
	 */
	public List<ProductCategory> getPlatformChildrenProductCategoryList(ProductCategory productCategory) {
		String sql = "select * from mall_product_category  where id != ? and path like ? and uid='"+ CommonUtil.PLATFROM_FLAG+"' order by orderList asc";
		return dao.find(sql,productCategory.getId(),productCategory.getId() + "%");
	}

	/**
	 * 根据ProductCategory对象获取当前商户所有子类集合，若无子类则返回null;
	 *
	 * @return 子类集合
	 *
	 */
	public List<ProductCategory> getBusinessChildrenProductCategoryList(String uid, ProductCategory productCategory) {
		String sql = "select * from mall_product_category  where uid=? and id != ? and path like ? order by orderList asc";
		return dao.find(sql,uid,productCategory.getStr("id"),productCategory.getStr("id") + "%");
	}
	
	public boolean equals(ProductCategory obj) {
		if (obj == null)
			return false;
		return getStr("id").equals(obj.getStr("id"));
	}


}
